package com.example.ormsqlsplicingframe1.orm.wrapper;

import java.util.ArrayList;
import java.util.List;

public class LeftJoin {

    private List<String> list = new ArrayList<>();

    public LeftJoin leftTitle(String fileTitle,String on){
        list.add(" left join "+fileTitle+" on "+on+" ");
        return this;
    }


    public LeftJoin leftTitle(String overall){

        int i = overall.indexOf(",");

        if (i == -1){
            list.add(" left join "+overall+" ");
            return this;
        }
        String fileTitle = overall.substring(0, i);

        String on = overall.substring(i+1);

        list.add(" left join "+fileTitle+" on "+on+" ");
        return this;
    }

    public LeftJoin leftTitle(SelectWrapper wrapper, String asName){

        String sql = wrapper.getSql();
        list.add(" left join ("+sql+" ) "+asName);
        return this;
    }

    /**
     *
     * @param wrapper 子查询
     * @param on   别名
     * @param onName  连接条件
     * @return
     */
    public LeftJoin leftTitle(SelectWrapper wrapper, String on, String onName){

        String sql = wrapper.getSql();
        list.add(" left join ("+sql+" ) "+on+" "+onName);
        return this;
    }

    public String getSql(){
        StringBuffer buffer = new StringBuffer();
        if (list != null && list.size()>0){
            for (final String s : list) {
                buffer.append(s);
            }
        }

        return buffer.toString();
    }

}
